﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2014-04-18
//
// Update Date :            
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


class FU_RU_ITEM;
class FU_RU_BAR;
class FUI_Rollup;
typedef FUI_Vector< FU_RU_ITEM* > FV_UI_PRU_ITEM;

class FAPI FU_RU_ITEM : public IFUI_MemObject
{
    FUI_DECLARE_CLASS(FU_RU_ITEM);

    friend class FUI_Rollup;

protected:
    FU_RU_ITEM* pParent;
    FV_UI_PRU_ITEM vChild;
    int nHeight;            // 本项高度
    bool bOpen;             // 打开状态
    bool bSelect;           // 当前是否是选中项
    bool bSuspend;          // 当前是否是悬停项

public:
    WCHAR szDrawStr[FUEL_UI_DRAW_MAX];  // 渲染时显示的内容
    WCHAR szNote[32];       // 备注 (控件不用，由使用者控制)
    int nID;                // ID号 (控件不用，由使用者控制)
    DWORD_PTR dwDataPtr;    // 数据 (控件不用，由使用者控制)

    FU_RU_ITEM();
    virtual~FU_RU_ITEM();
    virtual bool IsBar(){ return false; }
    virtual bool IsRoot(){ return false; }
    virtual int GetDrawHeight(FUI_Rollup* pru);
    virtual FU_RU_ITEM* AddChild(FU_RU_ITEM* p);
    virtual bool DelChild(FU_RU_ITEM* p, bool del_it);
    virtual void Clear();

    const WCHAR* SetDrawStr(const WCHAR* p);
    FU_RU_BAR* GetTopBar();

    FINLINE int SetHeight(int n){ if (n > 5) nHeight = n; return nHeight; }
    FINLINE FU_RU_ITEM* GetParent(){ return pParent; }
    FINLINE int GetMyHeight(){ return nHeight; }
    FINLINE bool IsOpen(){ return bOpen; }
    FINLINE bool IsSelect(){ return bSelect; }
    FINLINE bool IsSuspend(){ return bSuspend; }
    FINLINE bool HasChild(){ return !vChild.empty(); }
    FINLINE size_t Count(){ return vChild.size(); }
    FINLINE FU_RU_ITEM* Get(size_t idx){ return vChild[idx]; }

protected:
    bool Draw(FUI_Rollup* pru, fuPoint& pt, funit indent, fuRect& dst, bool text, size_t cross);
    FU_RU_ITEM* Find(FUI_Rollup* pru, fuPoint& pt, funit indent, fuRect& dst, fuPoint& tst);
};

class FAPI FU_RU_BAR : public FU_RU_ITEM
{
    FUI_DECLARE_CLASS(FU_RU_BAR);

public:
    virtual bool IsBar(){ return true; }
};
typedef FUI_Vector< FU_RU_BAR* > FV_UI_PRU_BAR;

class FAPI FU_RU_ROOT : public FU_RU_ITEM
{
    FUI_DECLARE_CLASS(FU_RU_ROOT);

public:
    FU_RU_ROOT(){ bOpen = true; vChild.reserve(8); }
    virtual bool IsRoot(){ return true; }
};


// 自定义渲染接口
// .返回自定义渲染是否成功绘制，否则UI系统按默认绘制
typedef bool (*FUI_Rollup_Render)(FUI_Rollup* pctrl, fuRect* rc, FU_RU_ITEM* it, bool non_text);


class FAPI FUI_Rollup : public FUI_Object
{
    friend class FU_RU_ITEM;

    FUI_DECLARE_CLASS(FUI_Rollup)

public:
    FUI_Rollup(EF_UI_WIDGET_TYPE t = eUWT_Rollup);
    virtual void Release();
    virtual void Render(EF_UI_RENDER_LAYER layer);
    virtual void UpdateRect();
    virtual bool IsChild(FUI_Object* pChild);
    virtual void SetHeight(funit h);
    virtual bool HasClip(){ return vBarRoot.HasChild(); }
    virtual bool OnMouseWheel(bool bUp, funit nX, funit nY);
    virtual bool OnMouseMove(funit nX, funit nY);
    virtual bool OnLButtonDown(funit nX, funit nY);
    virtual bool OnLButtonUp(funit nX, funit nY);
    virtual void OnFrameHide(bool visable);

    FU_RU_BAR* AddBar(const WCHAR* str, bool open_it = false);
    FU_RU_BAR* AddBar(const ACHAR* str, bool open_it = false);
    FU_RU_ITEM* AddItem(const WCHAR* str, FU_RU_ITEM* father);
    FU_RU_ITEM* AddItem(const ACHAR* str, FU_RU_ITEM* father);
    FU_RU_ITEM* OpenItem(FU_RU_ITEM* p, bool v);
    void Clear();
    void RenderBar(bool text);
    FU_RU_ITEM* SetSelect(FU_RU_ITEM* p);
    FU_RU_ITEM* SetSuspend(FU_RU_ITEM* p);
    FINLINE FU_RU_ITEM* GetSelect(){ return pItemSelect; }
    FINLINE FU_RU_ITEM* GetSuspend(){ return pItemSuspend; }

    FINLINE void SetRender(FUI_Rollup_Render p){ ruRender = p; }
    // 获取部分控件数据
    FINLINE FUI_ScrollBar& GetScroll(){ return uScrollBar; }

protected:
    virtual void InitUIData(FUI_Object* father, fuWidgetBase* pData, FUI_Layout* layout);
    virtual void Update();

protected:
    fuWidgetRollup* pRollupData;    // 控件数据地址
    FUI_ScrollBar uScrollBar;
    FU_RU_ROOT vBarRoot;            // 分类列表
    FU_RU_ITEM* pItemSelect;        // 当前选中的项
    FU_RU_ITEM* pItemSuspend;       // 当前鼠标悬停的项
    FUI_Rollup_Render ruRender;
};
